#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e4 + 10, INF = 2147483647;
vector<pii> as[N];
int dist[N];
bool st[N];
int n, m, s;
void ksk()
{
    for (int i = 0; i <= n; i++)
    {
        dist[i] = INF;
    }
    dist[s] = 0;
    for (int i = 1; i <= n; i++)
    {
        int a = 0;
        for (int j = 1; j <= n; j++)
        {
            if (!st[j] && dist[j] < dist[a])
            {
                a = j;
            }
        }
        st[a] = true;
        for (auto& e : as[a])
        {
            int b = e.first;
            int c = e.second;
            if (dist[a] + c < dist[b])
            {
                dist[b] = dist[a] + c;
            }
        }
    }
}
int main()
{
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        as[u].push_back({ v,w });
    }
    ksk();
    for (int i = 1; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
    return 0;
}